using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace CodePlex.Resourceful.Amazon.S3
{
    public class BucketLoggingStatus
    {

        public static readonly BucketLoggingStatus Disabled = new BucketLoggingStatus(false, null, null);

        private readonly bool _isEnabled;
        private readonly string _targetBucket;
        private readonly string _targetPrefix;

        private BucketLoggingStatus(bool isEnabled, string targetBucket, string targetPrefix)
        {
            _isEnabled = isEnabled;
            _targetBucket = targetBucket;
            _targetPrefix = targetPrefix;
        }

        public bool IsEnabled
        {
            get
            {
                return _isEnabled;
            }
        }

        public string TargetBucket
        {
            get
            {
                return _targetBucket;
            }
        }

        public string TargetPrefix
        {
            get
            {
                return _targetPrefix;
            }
        }

       

        public static BucketLoggingStatus Enabled(string targetBucket, string targetPrefix)
        {
            return new BucketLoggingStatus(true, targetBucket, targetPrefix);
        }

        public static BucketLoggingStatus Parse(XmlReader reader)
        {
         
            bool isEnabled = false;
            string targetBucket = null;
            string targetPrefix = null;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "LoggingEnabled")
                    {
                        isEnabled = true;
                       
                    }
                    else if (reader.Name == "TargetBucket")
                    {
                        targetBucket = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "TargetPrefix")
                    {
                        targetPrefix = Utils.ReadString(reader);
                    }
                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "BucketLoggingStatus")
                {
                    return isEnabled ? BucketLoggingStatus.Enabled(targetBucket, targetPrefix) : BucketLoggingStatus.Disabled;
                }

            }

            throw CodePlexException.Format("Failed to parse bucket-logging-status.");

        }


    }
}
